package com.tjoris.bpmstudio;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

class BpmPlayPauseAction extends AbstractBpmOnAction
{
	private final URL fGetState;
	private final URL fPlayURI;
	private final URL fPauseURI;
	
	
	public BpmPlayPauseAction(final String id, final String name, final String playerNumber) throws MalformedURLException
	{
		super(id, name);
		fGetState = new URL(kBASE_URL + "GETPLAYSTATE" + playerNumber);
		fPlayURI = new URL(kBASE_URL + "PLAY" + playerNumber);
		fPauseURI = new URL(kBASE_URL + "PAUSE" + playerNumber);
	}
	
	public boolean isVariable()
	{
	    return false;
	}
	
	public void performAction(final int value)
	{
		try
		{
			if (isPlaying())
			{
				fPauseURI.getContent();
			}
			else
			{
				fPlayURI.getContent();
			}
		}
		catch (final Exception e)
		{
			e.printStackTrace();
		}
	}
	
	private boolean isPlaying() throws IOException
	{
		return readData(fGetState) == 1;
	}
}
